<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    protected $_request;

    public function __construct(Request $request) {
        $this->_request = $request;

    }

    protected function _outPut( $data, $code = 0, $msg = 'ok', $view = '' ) {
        return $this->_apiOutPut($code, $msg,$data);
    }


    protected function _apiOutPut($code, $msg,$data=[],  $http_code = 200) {
        $json_data = ['code' => (int) $code, 'msg' => (string) $msg, 'data' => (array) $data];

        if ($code) {
            $todayDate = date("Y-m-d");
            $uri = explode('?', $_SERVER['REQUEST_URI']);
            $timeStr = date('Y-m-d H:i:s');
            file_put_contents(storage_path("logs/api_error-{$todayDate}.log"),
                "[$timeStr] [{$uri[0]}][{$code}][{$msg}] ".json_encode($this->_request->input())."\n", FILE_APPEND);
        }
        return response()->json($json_data);
    }
}
